package gsw.common.login.loginFinger;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import gsw.common.R;
import gsw.tool.common.Tool;
import gsw.tool.common.ToolShow;

/**
 * 指纹登录界面
 */
public class LoginFingerPoupWindow {
    //显示PopupWindow的Activity
    Activity activity;
    PopupWindow window;
    TextView show;
    int times = 5;
    LoginFingerListener loginFingerListener;

    public LoginFingerPoupWindow(Activity activity) {
        this.activity = activity;
        this.window = create();
        this.loginFingerListener = new LoginFingerListener() {
            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                times = 5;
                show(errString);
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                show(helpString);
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
                times = 5;
                show("验证成功");
                dismissDelay();
            }

            @Override
            public void onAuthenticationFailed() {
                times--;
                show.setText("验证失败,还有" + times + "次机会");
            }
        };
    }

    /**
     * 创建PopupWindow
     */
    private PopupWindow create() {
        // 用于PopupWindow的View
        View contentView = createContentView();
        // 创建PopupWindow对象，其中：
        // 第一个参数是用于PopupWindow中的View，第二个参数是PopupWindow的宽度，
        // 第三个参数是PopupWindow的高度，第四个参数指定PopupWindow能否获得焦点
        window = new PopupWindow(contentView, 100, 100, true);
        //设置自适应宽高
        window.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        window.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置PopupWindow的背景
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(false);
        // 设置PopupWindow是否能响应点击事件
        window.setTouchable(true);
        //设置Poupwindow的背景为透明灰
        setBackground();
        return window;
    }

    /**
     * 创建PopupWindow的View
     */
    private View createContentView() {
        View contentView = LayoutInflater.from(activity).inflate(R.layout.poupwindow_login_finger, null, false);
        show = contentView.findViewById(R.id.show);
        View cancel = contentView.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return contentView;
    }

    /**
     * 设置Poupwindow的背景为透明灰
     */
    private void setBackground() {
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = 1.0f;
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(lp);
            }
        });

        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.5f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 显示PopupWindow
     */
    public void show() {
        // 显示PopupWindow，其中：
        //第一个参数是PopupWindow的父View，第二个参数是PopupWindow相对父View的位置，
        //第三和第四个参数分别是PopupWindow相对父View的x、y偏移
        window.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        if (checkFinger()) {
            LoginFingerHelper.getInstance(activity, loginFingerListener).startListen();
        }
    }

    /**
     * 检查是否支持指纹登录
     *
     * @return
     */
    private boolean checkFinger() {
        if (!LoginFingerHelper.getInstance(activity, loginFingerListener).isHardwareDetected()) {
            show.setText("当前设备不支持指纹登录");
            return false;
        }
        if (!LoginFingerHelper.getInstance(activity, loginFingerListener).hasEnrolledFingerprints()) {
            show.setText("没有可用的指纹,请先录入指纹");
            return false;
        }
        return true;
    }

    /**
     * 关闭PopupWindow
     */
    public void dismiss() {
        window.dismiss();
        LoginFingerHelper.getInstance(activity, loginFingerListener).stopListen();
    }

    /**
     * 文本提示
     */
    public void show(final CharSequence text) {
        show.post(new Runnable() {
            @Override
            public void run() {
                show.setText(text);
            }
        });
    }

    /**
     * 关闭PopupWindow:延迟300毫秒
     */
    public void dismissDelay() {
        show.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, 500);
    }
}
